﻿using System.Web.Mvc;
using KMF.DAL;
using KMF.Utility;
using KMF.WebUI.Infrastructure;
using KMF.WebUI.Models;
using System;
using KMF.WebUI.Areas.KMFadmin.Models;

namespace KMF.WebUI.Areas.KMFadmin.Attributes
{
    public class AdminLoggedAuthorize : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {

            var loginCookie = filterContext.HttpContext.Request.Cookies["loginCookie"];

            if (loginCookie != null)
            {
                var user = UserDb.GetByUserName(loginCookie["UserName"]);

                if (user != null)// neu user nay ton tai thi kiem tra mat khau
                {
                    var loginCookieGuid = loginCookie["loginCookieGuid"];
                    var password = loginCookie["loginCookiePass"];
                    if (password.CreatePasswordHash(loginCookieGuid) == loginCookie["cookiesHash"] &&
                        user.Password.Equals(password)) // kiem tra mat khau va cookie
                    {
                        //set session
                        if (filterContext.HttpContext.Session[SessionConfig.SessionAdminLogged] == null)
                        {
                            filterContext.HttpContext.Session[SessionConfig.SessionAdminLogged] = new AdminLoginModel
                            {
                                Id =
                                    Convert
                                    .ToInt32(
                                        loginCookie
                                    ["Id"]),
                                UserName =
                                    loginCookie[
                                        "UserName"
                                    ],
                                FullName =
                                    loginCookie[
                                        "FullName"
                                    ],
                                Email =
                                    loginCookie[
                                        "Email"]
                            };
                        }
                        return;
                    }
                    else // khong dung mat khau
                    {
                        loginCookie.Expires = DateTime.Now.AddDays(-1);
                        filterContext.HttpContext.Request.Cookies.Add(loginCookie);
                        filterContext.HttpContext.Session[SessionConfig.SessionAdminLogged] = null;
                    }
                }
                else // khong co user nay
                {
                    filterContext.HttpContext.Session[SessionConfig.SessionAdminLogged] = null;
                }

            }
            if (filterContext.HttpContext.Session[SessionConfig.SessionAdminLogged] == null)
            {
                filterContext.Result = new RedirectResult("/AdminLogin/Login");
            }
        }
    }
}